'''
Created on 2011-1-30

@author: Q
'''

import os, sys, string
from Util import *;

def getHostName():
    file = os.popen("hostname")
    lines = file.readlines()
    hostname = string.strip(lines[0])
    return hostname

#def getBinaryDirectoryPath():
#    hostname = getHostName()
#    path = "../"
#    if hostname.find("siqi") >=0:
#        if sys.platform.find("win") >=0:
#            path = "../"
#        elif sys.platform.find("linux")>=0:
#            path = "/root/1.0.5_modified/bin/"
#    else:
#        if sys.platform.find("linux") >=0:
#            path = "/root/.openttd/"
#    return path

def getBinaryDirectoryPath():
        ret = "";
        hostname = getHostName()
        filename = os.path.join("..", "conf","main.conf")
        file = open(filename)
        lines = file.readlines();
        l = []
        for line in lines:
            if line.find("#") != 0:
                tup = string.split(line,maxsplit=1);
                if(tup[0] == "binaryDirectoryPath"):
                    ret = string.strip(tup[1]);
        return ret;

def getBinaryDirectoryForRunPath():
        ret = "";
        hostname = getHostName()
        filename = os.path.join("..", "conf","main.conf")
        file = open(filename)
        lines = file.readlines();
        l = []
        for line in lines:
            if line.find("#") != 0:
                tup = string.split(line,maxsplit=1);
                if(tup[0] == "binaryRunPath"):
                    ret = string.strip(tup[1]);
        return ret;

"""
the value return by this function is different from the one returned in Configuration.getAutoSave()
"""
def getAutoSavePath():
#    if sys.platform == "win32":
#            autoSavePath = "C:\\Users\\Q\\Documents\\OpenTTD\\save\\autosave"
#    elif sys.platform.find("linux") >= 0:
    try:
        autoSavePath = os.path.join(rt(getBinaryDirectoryForRunPath()), "save", "autosave")
        autoSavePath = fm(autoSavePath);
        return autoSavePath
    except:
        print "getAutoSavePath " + os.getcwd();
        return None
        
            
def getGameStartScrPath():
    path = os.path.join(rt(getBinaryDirectoryPath()), "scripts", "game_start.scr");
    path = fm(path);
    return path

def getFileName():
    path = os.path.join(rt(getBinaryDirectoryPath()), "openttd.cfg");
    path = fm(path);
    return path

def getNameWithReplayPrefix(expName):
#    if expName.find("replay-")>=0:
#        pos = expName.find("replay-") + len("replay-")
#        expName = expName[pos:]
#    if expName.find("quick-")>=0:
#        pos = expName.find("quick-") + len("quick-")
#        expName = expName[pos:]
        
    return expName

if __name__ == "__main__":
    for expName in ["1a1", "replay-1a1", "replay-1a7", "quick-1a11"]:
        print getNameWithReplayPrefix(expName)